<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
define('PER_PAGE', 12);
class dish extends Front_Controller {
	function __construct()
	{
		parent::__construct();
		$this->load->model('dish/dish_model');
		
		$this->load->helper('breadcrumb');
	}

	function index() {
		$this->_forward('dish_category');
	}

	function dish_category ( $category_alias = false ) {
		$per_page = 9;

		$page = $this->input->get_param('page', 0);
		$offset = ($page > 0)?($page-1) * $per_page : 0;

		$filter = array();
		$dish = array();
		$total = 0;
		if ($category_alias){
			$this->load->model('categories/categories_model');
			$category = $this->categories_model->get_category_by_alias($category_alias);
			if ($category){
				$filter = array('dish_category'=>$category['category_id']);
			}

			$filters       = array_merge(array('status'=>1,'page'=>$offset,'limit'=>$per_page, 'frontend' => 1), $filter);
			$data_dish     = $this->dish_model->get_dishs($filters);
			$count_filters = array_merge(array('status'=>1),$filter);
			$total         = $this->dish_model->get_count($count_filters);
		} else {

			$filters = array('status'=>1, 'page'=>$offset, 'limit'=>3, 'limit'=>$per_page, 'frontend' => 1);
			$data_dish = $this->dish_model->get_dishs($filters);
			$count_filters = array_merge(array('status'=>1),$filter);
			$total = $this->dish_model->get_count($count_filters);
		}

		if (is_array($data_dish) && count($data_dish)>0){
			$this->load->library('pagination');
			$config                         = array();
			$config['total_rows']           = $total;
			$config['per_page']             = $per_page;
			$config['num_links']            = '5';
			$config['page_query_string']    = TRUE;
			$config['query_string_segment'] = 'page';
			$config['use_page_numbers']     = TRUE;
			$this->pagination->initialize($config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign('data_dish', $data_dish);
			$this->smarty->assign('pagination', $pagination);
		}

		if ($category_alias) {
			$this->smarty->assign('box_title', $category['name']);
			$this->smarty->assign('page_title', "Hướng dẫn nấu các " . $category['name'] . " ngon tuyệt");

			$page_description = $category['name'] . " - MonGiNgon.vn Hướng dẫn nấu các " .$category['name'] .
			" ngon tuyệt! (món ngon cuối tuần, các món ăn ngon dễ làm, các món ăn hàng ngày, món chay ngon, cách làm bánh...)";

			$this->smarty->assign('page_description', $page_description);
			$this->smarty->assign('page_url', $category['link']);
			$this->smarty->assign('page_keywords', $category['name']);
		} else {
			$title = "Các món ngon hằng ngày, món ngon cuối tuần";
			$this->smarty->assign('box_title', $title);
			$this->smarty->assign('page_title', $title);
			$this->smarty->assign('page_url', get_url('dish'));
		}

		$this->config->load('breadcrumb');
		if ($category_alias && isset($category)) {
			$this->config->set_item('replacer_embed', array($category_alias => $category['name']));
		}
		
		$set_breadcrumb = set_breadcrumb();
		$this->smarty->assign('set_breadcrumb', $set_breadcrumb);

		$this->smarty->display('dish_list');
	}

	function dish_detail($dish_alias) {

		$dish = $this->dish_model->get_dish_by_alias($dish_alias);

		if ($dish) {
			//tags
			$arr_tags = explode(",", $dish['tags']);
			$tags = null;
			if (is_array($arr_tags)) {
				foreach ($arr_tags as $key => $tag) {
					$tags[$key]['name'] = $tag;
					$tags[$key]['value'] = alias_url($tag);
				}
			}
			
			$this->smarty->assign('tags', $tags);
			
			//var_dump($tags);


			$this->dish_model->update_dishs_view($dish['dish_id']);

			$filter = array('other_dish_id'=>$dish['dish_id'],'dish_category'=>$dish['category_id'], 'limit'=>9 ,'status'=>1, 'dish_id_other' => $dish['dish_id']);
			$other_dish = $this->dish_model->get_dishs($filter, false);
			$dish['stocks'] = $this->dish_model->get_stocks($dish['dish_id']);
			$dish['steps'] = $this->dish_model->get_steps($dish['dish_id']);
			$this->smarty->assign('other_dish',$other_dish);
			$this->smarty->assign('dish', $dish);

			// === user ===
			$this->load->model('users/users_model');
			$user = $this->users_model->getUser($dish['user_id']);
			$this->smarty->assign('user', $user);
			// === end  ===
			$array_breadcrumbs = array();
			$array_breadcrumbs[$dish['alias']] = $dish['title'];
			$category_alias = $this->uri->segment(2);
			$category['name'] = 'Món Gì Ngon';
			if ($category_alias) {
				$this->load->model('categories/categories_model');
				$category = $this->categories_model->get_category_by_alias($category_alias);
				$array_breadcrumbs[$category['alias']] = $category['name'];
			}

			//--
			//show category in id
			$array_categories = array(135,136,137,141,139);
			$data_tab = array();
			foreach ($array_categories as $category_id) {
				$category_tab = $this->dish_model->get_categories(array('category_id' => $category_id, 'status' => 1), false, false);
				$data_tab[$category_id] = $category_tab[0];
				$data_tab[$category_id]['sub'] =  $this->dish_model->get_dishs(array('limit' => 8, 'category_id' => $category_id, 'frontend' => 1, 'status' => 1));
			}
			$this->smarty->assign('data_tab', $data_tab);
			//--//

			$this->config->load('breadcrumb');
			$this->config->set_item('replacer_embed', $array_breadcrumbs);
			$set_breadcrumb = set_breadcrumb();
			$this->smarty->assign('set_breadcrumb', $set_breadcrumb);

			//---------//
			$this->smarty->assign('page_title', $dish['title'] . ' ['. $category['name'] . ']');
			$this->smarty->assign('page_description', $dish['introtext']);
			$this->smarty->assign('page_url', $dish['link']);
			$this->smarty->assign('page_image', $dish['image1']);
			$this->smarty->assign('page_keywords', $dish['tags']);
		}

		$this->smarty->display('dish_detail');
	}

	function dish_public() {
		$dish = $this->dish_model->update_dishs_public();
	}

	function dish_tag ( $tags = false ) {
		$this->config->load('breadcrumb');
		$per_page = 9;

		$page = $this->input->get_param('page', 0);
		$offset = ($page > 0)?($page-1) * $per_page : 0;

		$filter = array();
		$dish = array();
		$total = 0;
		if ($tags) {
			$this->load->model('categories/categories_model');
			$category = $this->categories_model->get_category_by_alias($tags);
			if ($category) {
				$filter = array('dish_category'=> $category['category_id']);
			} else {

				$tag = str_replace('-', ' ', $tags);
				$tag = trim($tag);
				$filter = array('tags'=> $tag);
			}

			$filters       = array_merge(array('status'=>1,'page'=>$offset,'limit'=>$per_page, 'frontend' => 1), $filter);
			$data_dish     = $this->dish_model->get_dishs($filters);
			$count_filters = array_merge(array('status'=>1), $filters);
			$total         = $this->dish_model->get_count($count_filters);
		}

		if (is_array($data_dish) && count($data_dish)>0) {
			$this->load->library('pagination');
			$config                         = array();
			$config['total_rows']           = $total;
			$config['per_page']             = $per_page;
			$config['num_links']            = '5';
			$config['page_query_string']    = TRUE;
			$config['query_string_segment'] = 'page';
			$config['use_page_numbers']     = TRUE;
			$this->pagination->initialize($config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign('data_dish', $data_dish);
			$this->smarty->assign('pagination', $pagination);
		}

		if ($tag) {
			$title = 'Từ khóa : ' . $tag;
			$this->smarty->assign('box_title', $title);
			$this->smarty->assign('page_title', $title);
			$this->smarty->assign('page_keywords', $tag);

			$page_description = 'Tìm kiếm với từ khóa : '. $tag . " - Hướng dẫn nấu các món ngon cuối tuần...";

			$this->smarty->assign('page_description', $page_description);
			$this->smarty->assign('page_url', $tag);

			$array_breadcrumbs = array();
			$array_breadcrumbs[$tags] = $title;
			$this->config->set_item('replacer_embed', $array_breadcrumbs);
		}
		
		$set_breadcrumb = set_breadcrumb();
		$this->smarty->assign('set_breadcrumb', $set_breadcrumb);

		$this->smarty->display('dish_list');
	}
}
